﻿@{
    ViewData["Title"] = "添加";
}

<form id="myFormId" target="frmright">
    <input type="hidden" name="editType" value="@ViewData["editType"]" />
    <input type="hidden" name="partId" value="@ViewData["partId"]" />
    <input type="hidden" name="partType" id="partType" value="@ViewData["partType"]" />
    <div class="basicTab">
        <div name="采购单">
            <table class="tableStyle" formMode="view">
                <tr>
                    <td>单据类型</td>
                    <td>
                        <select prompt="请选择" url="/wms/PurchaseManage/GetOrderType" name="orderType" id="orderType">
                        </select><span class="star">*</span>
                    </td>
                    <td>供应商代码</td>
                    <td>
                        <input id="vendorName" name="vendorName" value="@ViewData["vendorName"]" class="autoComplete" url="/wms/MovementInstockManage/GetfileVendor"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>采购日期</td>
                    <td> <input type="text" id="seDate" name="seDate" class="date" /><span class="star">*</span></td>
                    <td>
                        采购时间：
                    </td>
                    <td>
                        <input type="text" id="seTime" name="seTime" style="width:auto" class="date" dateFmt="HH:mm:ss" /><span class="star">*</span>
                    </td>
                </tr>
                <tr>
                    <td>道口号</td>
                    <td><input type="text" name="dock" id="dockNo" value="@ViewData["dock"]" /></td>
                    <td>是否可用</td>
                    <td><select selectedValue="@ViewData["enabled"]" name="enabled" id="enabled" data='{"list":[{"value":"Y","key":"是"},{"value":"N","key":"否"}]}'></select></td>
                </tr>
            </table>
        </div>
        <div name="采购单明细">
            <table class="tableStyle" formMode="view">
                <tr>
                    <td>零件号</td>
                    <td>
                        <input id="partNo" name="partNo" value="@ViewData["partNo"]" class="autoComplete" url="/LocatePartDefine/GetPart_no"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                    <td>简码</td>
                    <td>
                        <input id="partSpec" name="partSpec" value="@ViewData["partSpec"]" class="autoComplete" url="/MovementInstockManage/GetPart_Spec"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>数量</td>
                    <td><input type="text" name="qty" value="@ViewData["qty"]" id="qty" /><span class="star">*</span></td>
                    <td>库存地</td>
                    <td><select selectedValue="@ViewData["erpWarehouse"]" prompt="请选择" url="/wms/PurchaseManage/GetErpwarehouse" id="erpWarehouse" name="erpWarehouse"></select><span class="star">*</span></td>

                </tr>
            </table>
            <input type="button" value="添加" onclick="return onAdd()" />&nbsp;&nbsp;<input type="button" value="删除" onclick="ondelete()" />
        </div>
    </div>
    <div>
        <div class="basicTab" id="myTab">
            <div name="零件号列表">
                <div id="maingrid"></div>
            </div>
        </div>
        <table class="tableStyle" formMode="transparent">
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交" onclick="return add()" />
                    <input type="button" value="取消" onclick="top.Dialog.close()" />
                </td>
            </tr>
        </table>
    </div>
</form>
<!-- 异步提交start -->
<script type="text/javascript">
    var grid;
    var indexVal = 0;
    function initComplete() {

        var date = new Date(); //获取完整的日期
        var year = date.getFullYear(); //获取当前年份
        var month = date.getMonth() + 1;//获取当前月份
        var day = date.getDate();//获取当前日
        month = (month > 9) ? month : ("0" + month);
        day = (day < 10) ? ("0" + day) : day;
        var today = year + "-" + month + "-" + day;
        $("#seDate").val(today);

        var hour = date.getHours(); //获取 时
        var minutes = date.getMinutes(); //获取 分
        var seconds = date.getSeconds() //获取 秒
        var timeStr = hour + ":" + minutes + ":" + seconds;
        $("#seTime").val(timeStr);


        grid = $("#maingrid").quiGrid({
            columns: [
                { display: '零件号', name: 'partNo', align: 'center', width: "25%" },
                { display: '简码', name: 'partSpec', align: 'center', width: "25%" },
                { display: '数量', name: 'qty', align: 'center', width: "25%" },
                { display: '库存地', name: 'erpWarehouse', align: 'center', width: "25%" }
            ],
            data: [], sortName: 'orderNo', rownumbers: true, checkbox: true, percentWidthMode: true,
            height: '94%', width: "100%"
        });

        $("#partNo").bind("change", function() {
            var partNo = $("#partNo").val();
            //if (partNo != "" && partNo != null) {
            //    $("#partSpec").val("");
            //    return;
            //}
        })

        $("#partSpec").bind("change", function() {
            var partSpec = $("#partSpec").val();
            //if (partSpec != "" && partSpec != null) {
            //    $("#partNo").val("");
            //    return;
            //}
        })

        //表单提交
        $('#myFormId').submit(function() {
            debugger
            var orderType = $("#orderType").val()
            if (orderType == "" || orderType == null) {
                alert("请选择单据类型！");
                return;
            }
            $.post("/wms/PurchaseManage/savePurchaseManage",
                {
                    "orderType": $("#orderType").val(), "vendorName": $("#vendorName").val(), "seDate": $("#seDate").val(),
                    "seTime": $("#seTime").val(), "dock": $("#dockNo").val(), "enabled": $("#enabled").val(),
                    "detailRow": JSON.stringify(grid.rows)
                },
                function(result) {
                    top.Dialog.alert(result.message, function() {
                        closeWin();
                    })
                });

            return false;
        });
    }
    var detailList = [];
    function onAdd() {
        var partNo = $("#partNo").val();
        var partSpec = $("#partSpec").val();
        if ((partNo == "" || partNo == null) && (partSpec == "" || partSpec == null)) {
            alert("请选择零件号或简码！");
            return;
        }

        var qty = $("#qty").val()
        if (qty == "" || qty == null) {
            alert("请输入数量！");
            return;
        }

        var erpWarehouse = $("#erpWarehouse").val()
        if (erpWarehouse == "" || erpWarehouse == null) {
            alert("请选择库存地！");
            return;
        }

        //输入零件号或简码
        if (partNo != "" && partNo != null) {
            var partNo = $("#partNo").val();
            $.post("/MovementInstockManage/getPartNoInfo", { "partNo": partNo }, function (result) {
                debugger
                if (result.length == 0) {
                    alert("零件号不存在，请检查！");
                    return;
                }
                let gridArr = grid.rows;
                for (let i = 0, len = gridArr.length; i < len; i++) {
                    if (gridArr[i].partNo == partNo && gridArr[i].erpWarehouse == $('#erpWarehouse').val())//判断存在不添加
                    {
                        alert("库存地及零件号已添加，请检查！");
                        return;
                    }
                }

                detailList.push({
                    index: indexVal,
                    partNo: result[0].partNo,
                    partSpec: result[0].partSpec,
                    qty: $('#qty').val(),
                    erpWarehouse: $('#erpWarehouse').val()
                });

                var detailListPush = [];
                detailListPush.push({
                    rows: detailList
                })

                grid.loadData(detailListPush[0]);
                indexVal++;
                $("#partNo").val('');
                $("#partSpec").val('');
                $("#qty").val('');
                $("#erpWarehouse").value('');

            }, "json");
        }
        else if (partSpec != "" && partSpec != null) {
            var partSpec = $("#partSpec").val();
            $.post("/MovementInstockManage/getPartSpecInfo", { "partSpec": partSpec }, function (result) {
                debugger
                if (result.length == 0) {
                    alert("零件号不存在，请检查！");
                    return;
                }
                let gridArr = grid.rows;
                for (let i = 0, len = gridArr.length; i < len; i++) {
                    if (gridArr[i].partNo == result[0].partNo && gridArr[i].erpWarehouse == $('#erpWarehouse').val())//判断存在不添加
                    {
                        alert("库存地及零件号已添加，请检查！");
                        return;
                    }
                }

                detailList.push({
                    index: indexVal,
                    partNo: result[0].partNo,
                    partSpec: result[0].partSpec,
                    qty: $('#qty').val(),
                    erpWarehouse: $('#erpWarehouse').val()
                });

                var detailListPush = [];
                detailListPush.push({
                    rows: detailList
                })

                grid.loadData(detailListPush[0]);
                indexVal++;
                $("#partNo").val('');
                $("#partSpec").val('');
                $("#qty").val('');
            }, "json");
        }

    }

    function ondelete() {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";
        debugger
        for (var i = 0; i < selectedRowsLength; i++) {
            grid.deleteRow(selectedRows[i]);
            grid.loadData({ rows: grid.rows });
        }
    }

    //判断用户输入是否正确
    function add() {
        var orderType = $("#orderType").val();
        if (orderType == "" || orderType == null) {
            alert("请选择单据类型！");
            return false;
        }
        var seDate = $("#seDate").val();
        if (seDate == "" || seDate == null) {
            alert("请输入采购日期！");
            return false;
        }

        var seTime = $("#seTime").val();
        if (seTime == "" || seTime == null) {
            alert("请输入采购时间！");
            return false;
        }

        var rowNum = grid.getData().length;
        if (rowNum == 0) {
            alert("请生成采购单单明细！");
            return false;
        }
        return true;
    }

    //重置
    function closeWin() {
        //刷新数据
        var tabfrmId = "page_" + top.frmright.getCurrentTabId();
        top.frmright.document.getElementById(tabfrmId).contentWindow.refresh(false);
        //关闭窗口
        top.Dialog.close();
    }
</script>
<!-- 异步提交end -->	